home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / Drag_on Modules / hAWK programs / $FromClipOrFront < prev    next >
Text File  |  1996-05-25  |  2KB  |  82 lines

  1. # $FromClipOrFront: an example program that shows how a hAWK program
  2. # can be designed to take its input from either the front text window
  3. # (if you run it through the setup dialog) or from the clipboard
  4. # (if you run it via a command line).
  5. # The variable "fromFront" is set to 1 in the "Set variables"
  6. # dialog for this program, and the command line should read
  7. # "hAWK -f$FromClipOrFront -vfromFront=0 -n"
  8. # to signal that input will be from the clip (and no showing of stdout).
  9.  
  10. # To make this little example more interesting, we send results
  11. # to stdout if fromFront == 1, and put them back on the clip
  12. # if fromFront == 0. In order to tell something happened,
  13. # ProcessInputLine() strips the first character from each input line
  14. # (useless I know, but visually effective).
  15.  
  16. BEGIN {
  17.     while (GetLine() > 0)     # not getline, GetLine is a function just below
  18.         {
  19.         ProcessInputLine();
  20.         PrintOut();
  21.         }
  22.     FinishUp();
  23.     }
  24.  
  25. # Get the next line to $0 from file or clipboard.
  26. function GetLine(    retval)
  27.     {
  28.     if (fromFront == 1)
  29.         retval = getline;         # use supplied input file(s)
  30.     else
  31.         retval = getClipLine(); # use the clip
  32.     return retval;
  33.     }
  34.  
  35. # Split up the clip first time, return next line from clip.
  36. # Return values mimic the behaviour of getline.
  37. function getClipLine()
  38.     {
  39.     # First time, get the clip and split it into lines.
  40.     if (clip == "")
  41.         {
  42.         clip = getclip();
  43.         numLines = split(clip, lines, "\r");
  44.         # With "split", the last line will be empty if the last
  45.         # character copied was a return.
  46.         if (lines[numLines] == "")
  47.             --numLines;
  48.         if (numLines <= 0)
  49.             return -1;
  50.         }
  51.     ++currentLine;
  52.     if (currentLine > numLines)
  53.         return 0;
  54.     $0 = lines[currentLine];
  55.     return 1;
  56.     }
  57.  
  58. # Strip first character from input line.
  59. function ProcessInputLine()
  60.     {
  61.     $0 = substr($0, 2);
  62.     }
  63.  
  64. # Send output to stdout or variable out.
  65. function PrintOut()
  66.     {
  67.     if (fromFront == 1)
  68.         print $0;
  69.     else
  70.         out = out $0 "\r"; # or out = out "\r" $0, your choice
  71.     }
  72.  
  73. # If sending to clip, do so at end.
  74. function FinishUp()
  75.     {
  76.     if (fromFront == 1)
  77.         ;        # we're done
  78.     else        # put result back on the clip
  79.         {
  80.         putclip(out);
  81.         }
  82.     }